﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ActivityTracker.Library;
using Common.Library;


namespace ActivityTracker
{
    public partial class Categories : PhoneApplicationPage
    {
        #region global

        Category objCategory = new Category();

        #endregion

        #region constructor

        public Categories()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                ListBoxCategories.ItemsSource = objCategory.LoadCategories();
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In ActivityTracker.Views.Categories file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarDone_Click(object sender, EventArgs e)
        {
            try
            {
                objCategory.SaveCheckedCategories(ListBoxCategories);
                NavigationService.Navigate(new Uri("/Views/Activities/AddNewActivity.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarDone_Click Method In ActivityTracker.Views.Categories file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarNew_Click(object sender, EventArgs e)
        {
            PopupCategory.IsOpen = true;
        }

        private void AppBarDelete_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Categories/DeleteCategory.xaml", UriKind.Relative));
        }

        private void btnPopupAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string catName = objCategory.GetCategoryFromTextBox(PopupGrid);
                if (catName != "")
                {
                    txtblkreq.Visibility = Visibility.Collapsed;
                    PopupCategory.IsOpen = false;
                    txtcatName.Text = "";
                    objCategory.SaveNewCategory(catName);
                    ListBoxCategories.ItemsSource = objCategory.LoadCategories();
                }
                else
                {
                    txtblkreq.Visibility = Visibility.Visible;
                    PopupCategory.IsOpen = true;
                }

            }
            catch (Exception ex)
            {
                string mess = "Exception in btnPopupAdd_Click Method In ActivityTracker.Views.Categories file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void btnPopupCancel_Click(object sender, RoutedEventArgs e)
        {
            txtcatName.Text = "";
            txtblkreq.Visibility = Visibility.Collapsed;
            PopupCategory.IsOpen = false;
        }

        #endregion
    }
}